<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        //   Definition for singly-linked list.
        function ListNode(val, next) {
            this.val = (val === undefined ? 0 : val)
            this.next = (next === undefined ? null : next)
        }
        /**
         * @param {ListNode} head
         * @return {ListNode}
         */
        // var reverseList = function (head) {
        //     if (head == null || head.next == null) {
        //         return head
        //     }
        //     const res = reverseList(head.next);
        //     head = head.next.next;
        //     head.next = null;
        //     return res;

        // };

        var reverseList = function (head) {
            if (head == null || head.next == null) {
                return head;
            }
            const newHead = reverseList(head.next);
            head.next.next = head;
            head.next = null;
            return newHead;
        };
    </script>
</body>

</html>